import * as Cesium from 'cesium';

// 激光材质类
function LaserPolylineTrailLinkMaterialProperty(
    duration = 5000.0,
    color = new Cesium.Color(1.0, 0.0, 0.0, 0.5),
    imageCount = 10,       // 新增：图片重复个数
    imageWidth = 1.0,     // 新增：图片宽度缩放
    imageHeight = 1.0     // 新增：图片高度缩放
) {
    this._definitionChanged = new Cesium.Event();
    this._color = undefined;
    this._colorSubscription = undefined;
    this.color = color;
    this.duration = duration;
    this._time = new Date().getTime();
    
    // 新增属性
    this._imageCount = imageCount;
    this._imageWidth = imageWidth;
    this._imageHeight = imageHeight;
}

Object.defineProperties(LaserPolylineTrailLinkMaterialProperty.prototype, {
    isConstant: {
        get: function () {
            return false;
        },
    },
    definitionChanged: {
        get: function () {
            return this._definitionChanged;
        },
    },
    color: Cesium.createPropertyDescriptor('color'),
    // 新增属性的描述符
    imageCount: Cesium.createPropertyDescriptor('imageCount'),
    imageWidth: Cesium.createPropertyDescriptor('imageWidth'),
    imageHeight: Cesium.createPropertyDescriptor('imageHeight'),
});

LaserPolylineTrailLinkMaterialProperty.prototype.getType = function (time) {
    return 'PolylineTrailLink';
};

LaserPolylineTrailLinkMaterialProperty.prototype.getValue = function (time, result) {
    if (!Cesium.defined(result)) {
        result = {};
    }
    result.color = Cesium.Property.getValueOrClonedDefault(
        this._color,
        time,
        Cesium.Color.WHITE,
        result.color
    );
    result.image = Cesium.Material.PolylineTrailLinkImage;
    result.time = ((new Date().getTime() - this._time) % this.duration) / this.duration;
    
    // 传递新增属性的值
    result.imageCount = this._imageCount;
    result.imageWidth = this._imageWidth;
    result.imageHeight = this._imageHeight;
    
    return result;
};

LaserPolylineTrailLinkMaterialProperty.prototype.equals = function (other) {
    return (
        this === other ||
        (other instanceof LaserPolylineTrailLinkMaterialProperty &&
            Cesium.Property.equals(this._color, other._color) &&
            this._duration === other._duration &&
            // 比较新增属性
            this._imageCount === other._imageCount &&
            this._imageWidth === other._imageWidth &&
            this._imageHeight === other._imageHeight)
    );
};

Cesium.Material.PolylineTrailLinkType = 'PolylineTrailLink';
Cesium.Material.PolylineTrailLinkImage = new URL('/arrow.png', import.meta.url).href;
// 修复关键：将texture2D改为texture，并添加了新属性的使用
Cesium.Material.PolylineTrailLinkSource =
    `czm_material czm_getMaterial(czm_materialInput materialInput)\n\
 { czm_material material = czm_getDefaultMaterial(materialInput); vec2 st = materialInput.st;\n\
    // 使用imageCount控制图片重复次数，imageWidth和imageHeight控制缩放
    vec2 scaledSt = vec2(
        fract((st.s - time) * imageCount) * imageWidth,
        st.t * imageHeight
    );\n\
    vec4 colorImage = texture(image, scaledSt);\n\
     material.alpha = colorImage.a * color.a;\n\
     material.diffuse = (colorImage.rgb + color.rgb) / 2.0;\n\
     return material;}`;
Cesium.Material._materialCache.addMaterial(Cesium.Material.PolylineTrailLinkType, {
    fabric: {
        type: Cesium.Material.PolylineTrailLinkType,
        uniforms: {
            color: new Cesium.Color(1.0, 0.0, 0.0, 0.5),
            image: Cesium.Material.PolylineTrailLinkImage,
            time: 20,
            imageCount: 5,    // 新增uniform
            imageWidth: 1.0,  // 新增uniform
            imageHeight: 1.0  // 新增uniform
        },
        source: Cesium.Material.PolylineTrailLinkSource,
    },
    translucent: function (material) {
        return true;
    },
});

export default LaserPolylineTrailLinkMaterialProperty;
